package com.ruoyi.classai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.EwcChapterPptmaterial;
import com.ruoyi.classai.domain.dto.PublicIdQueryInterfaceDto;
import com.ruoyi.classai.mapper.EwcChapterPptmaterialMapper;
import com.ruoyi.classai.service.IEwcChapterPptmaterialService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 章节教学PPT材料Service实现类
 * 
 * @author ruoyi
 * @date 2025-03-02
 */
@Service
public class EwcChapterPptmaterialServiceImpl extends ServiceImpl<EwcChapterPptmaterialMapper, EwcChapterPptmaterial> implements IEwcChapterPptmaterialService {

    @Override
    public AjaxResult saveOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        if (StringUtils.isEmpty(publicIdQueryInterfaceDto.getMaterial())) {
            return AjaxResult.error("PPT材料内容不能为空");
        }

        EwcChapterPptmaterial pptmaterial = new EwcChapterPptmaterial();
        pptmaterial.setId(UUID.randomUUID().toString());
        pptmaterial.setClassId(publicIdQueryInterfaceDto.getClassId());
        pptmaterial.setCourseId(publicIdQueryInterfaceDto.getCourseId());
        pptmaterial.setChapterId(publicIdQueryInterfaceDto.getChapterUniqueId());
        pptmaterial.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        pptmaterial.setCreateTime(DateUtils.getTime());
        pptmaterial.setMaterial(publicIdQueryInterfaceDto.getMaterial());

        boolean result = this.save(pptmaterial);
        if (result) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @Override
    public boolean deleteOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        QueryWrapper<EwcChapterPptmaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", publicIdQueryInterfaceDto.getCourseId())
                .eq("chapter_id", publicIdQueryInterfaceDto.getChapterUniqueId())
                .eq("class_id", publicIdQueryInterfaceDto.getClassId())
                .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());
        
        return this.remove(queryWrapper);
    }

    @Override
    public boolean updateOne(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        if (StringUtils.isEmpty(publicIdQueryInterfaceDto.getMaterial())) {
            return false;
        }

        QueryWrapper<EwcChapterPptmaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", publicIdQueryInterfaceDto.getCourseId())
                .eq("chapter_id", publicIdQueryInterfaceDto.getChapterUniqueId())
                .eq("class_id", publicIdQueryInterfaceDto.getClassId())
                .eq("teacher_id", ThirdSessionHolder.getThirdSession().getId());

        EwcChapterPptmaterial pptmaterial = new EwcChapterPptmaterial();
        pptmaterial.setMaterial(publicIdQueryInterfaceDto.getMaterial());

        return this.update(pptmaterial, queryWrapper);
    }
} 